package com.yidu.service.impl;


import java.math.BigDecimal;

import java.util.List;

import java.util.Map;

import java.util.UUID;

import javax.annotation.Resource;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;

import com.yidu.dao.SnComInDetailMapper;

import com.yidu.dao.SnComSaDetailMapper;

import com.yidu.dao.SnComSalesMapper;

import com.yidu.dao.SnFiDetailMapper;

import com.yidu.dao.SnFinanceMapper;

import com.yidu.dao.SnReDetailMapper;


import com.yidu.dao.SnRetailMapper;

import com.yidu.model.SnComInDetail;

import com.yidu.model.SnComSaDetail;

import com.yidu.model.SnComSales;

import com.yidu.model.SnFiDetail;

import com.yidu.model.SnFinance;

import com.yidu.model.SnReDetail;

import com.yidu.model.SnRetail;

import com.yidu.model.SnUser;

import com.yidu.service.SnRetailService;

/** 
 * 
 * @ClassName: 零售订单service
 * @Description: 零售订单的实现类
 * @author: ZhangHao
 * @date: 2018年11月27日 上午11:08:35  
 */
@Service
public class SnRetailServiceImpl implements SnRetailService{
	/**
	 * 注入零售订单mapper
	 */
	@Resource
	SnRetailMapper retailMapper;
	/**
	 * 注入退货订单mapper
	 */
	@Resource
	SnComSalesMapper comSalerMapper;
	
	/**
	 * 注入退货详情mapper
	 */
	@Resource
	SnComSaDetailMapper saDetailMapper;
	
	/**
	 * 注入零售订单详情mapper
	 */
	@Resource
	SnReDetailMapper DetailMapper;
	
	/**
	 * 注入商品库存明细Mapper注入
	 */
	@Resource
	SnComInDetailMapper snComInDetailMapper;
	
	/**
	 * 注入财务Mapper
	 */
	@Resource
	SnFinanceMapper snFinanceMapper;
	
	/**
	 * 财务记录Mapper注入
	 */
	@Resource 
	SnFiDetailMapper snFiDetailMapper;
	
	/**
	 * 查询所有
	 */
	@Override
	public List<SnRetail> findAll(Map<String, Object> map) {
		//调用分页查询的方法
		return retailMapper.findAll(map);
	}
	
	/**
	 * 增加订单
	 */
	@Override
	public int add(String str,String sum,String memberId,HttpSession session) {
		//取到登陆的session
		SnUser user = (SnUser) session.getAttribute("user");
		//根据#号截取每行数据
		String[] string = str.split("#");
		//定义一个Integer变量
		Integer count = 0;
		
		//循环数据
		for (int i = 0; i < string.length; i++) {
			//把数据根据“，”分割
			String[] str2 = string[i].split(",");
			//定义取到零售数量
			String number = str2[7];
			//给count赋值
			count+=Integer.parseInt(number);
		}
		//根据￥取到总金额
		String sumMoney = sum.split("￥")[0];
		//建立随机数
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		//创建零售订单对象
		SnRetail snRetail = new SnRetail();
		//给零售ID赋值
		snRetail.setReId(uuid);
		//给分公司ID赋值
		snRetail.setBraId(user.getBraId());
		//给状态赋值
		snRetail.setReState(1);
		//创建大设计对象
		BigDecimal moneys = new BigDecimal(sumMoney);
		//给总金额赋值
		snRetail.setReSum(moneys);
		//创建大设计对象
		BigDecimal change = new BigDecimal(0);
		//找零赋值
		snRetail.setReChange(change);
		//是否有效赋值
		snRetail.setIsva(1);
		//时间赋值
		snRetail.setOptime(Tools.getCurDateTime());
		//操作人赋值
		snRetail.setOper(user.getUserName());
		//总数量赋值
		snRetail.setReCount(count);
		//会员ID赋值
		snRetail.setMeId(memberId);
		//调用增加订单的方法
		int rows = retailMapper.insertSelective(snRetail);
		
		//创建退货对象
		SnComSales comSales=new SnComSales();
		//退货id赋值
		comSales.setCsId(Tools.getDateOrderNo());
		//审核表id赋值
		comSales.setAllId(snRetail.getReId());
		//分公司赋值
		comSales.setBraId(user.getBraId());
		//给时间赋值
		comSales.setOptime(Tools.getCurDateTime());
		//操作人赋值
		comSales.setOper(user.getUserName());
		comSales.setSort(Integer.valueOf(Tools.getTimeStamp()));
		comSales.setCsState(0);
		comSales.setIsva(1);
		//调用退货增加的方法
		comSalerMapper.insertSelective(comSales);
		int rowa = 0;
		//循环数据
		for (int i = 0; i < string.length; i++) {
			//把数据根据，分割
			String[] str2 = string[i].split(",");
			//得到商品ID
			String comId = str2[0];
			//得到商品价格
			String money = str2[3];
			//得到商品总价
			String all = str2[5];
			//得到商品数量
			String number = str2[7];
			
			
			//创建退货详情
			SnComSaDetail saDetail=new SnComSaDetail();
			//退货id
			saDetail.setSdId(Tools.getDateOrderNo());
			saDetail.setCsId(comSales.getCsId());
			//状态
			saDetail.setIsva(1);
			//退货时间
			saDetail.setOptime(Tools.getCurDateTime());
			saDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			saDetail.setOper(comId);
			saDetail.setSdCount(Integer.valueOf(number));
			saDetail.setSdAlready(0);
			saDetail.setSdNotcount(0);
			saDetail.setSdState(0);
			saDetailMapper.insertSelective(saDetail);
			
			
			//修改库存数量
			retailMapper.updateInventory(comId, Integer.valueOf(number));
			String ciId = retailMapper.queryInventoryId(comId,user.getBraId());
			//创建库存明细对象
			SnComInDetail snComInDetail = new SnComInDetail();
			snComInDetail.setCidId(Tools.getDateOrderNo());
			//给库存ID赋值
			snComInDetail.setCiId(ciId);
			//给库存明细赋值
			snComInDetail.setCidExplain("零售"+str2[1]+" 库存减少"+number);
			//给是否有效赋值
			snComInDetail.setIsva(1);
			//给操作时间赋值
			snComInDetail.setOptime(Tools.getCurDateTime());
			//调用增加库存明细的方法	
			snComInDetailMapper.insertSelective(snComInDetail);
			
			
			//创建零售详情对象
			SnReDetail snReDetail = new SnReDetail();
			//给商品ID赋值
			snReDetail.setComId(comId);
			//给零售订单ID赋值
			snReDetail.setReId(snRetail.getReId());
			//创建大数据模型对象
			BigDecimal money2 = new BigDecimal(money);
			//给单价赋值
			snReDetail.setReMoney(money2);
			//给数量赋值
			snReDetail.setReNumber(Integer.valueOf(number));
			//建大数据模型对象
			BigDecimal allMoney = new BigDecimal(all); 
			
			//给小计赋值
			snReDetail.setReSum(allMoney);
			//是否有效赋值
			snReDetail.setIsva(1);
			//给时间赋值
			snReDetail.setOptime(Tools.getCurDateTime());
			//给操作人赋值
			snReDetail.setOper(user.getUserName());
			//调用销售详情增加的方法
			rowa = DetailMapper.insertSelective(snReDetail);
		}
		
		double moneyy = snRetail.getReSum().doubleValue();
		//调用根据分公司ID修改总财务金额的方法
		int rows3 = snFinanceMapper.updateMoney(user.getBraId(),moneyy);
		//调用根据分公司ID查询财务ID的方法
		SnFinance snFinance = snFinanceMapper.findByFinId(user.getBraId());
		//创建财务明细对象
		SnFiDetail snFiDetail = new SnFiDetail();
		//给财务ID赋值
		snFiDetail.setFinId(snFinance.getFinId());
		//创建大数据对象
		BigDecimal allMoneyy = new BigDecimal(moneyy);
		//给财务金额赋值
		snFiDetail.setFdMoney(allMoneyy);
		//给财务详情赋值
		snFiDetail.setFdExplain("零售商品增加财务金额"+allMoneyy+"元");
		//给操作人赋值
		snFiDetail.setOptime(Tools.getCurDateTime());
		//给是否有效赋值
		snFiDetail.setIsva(1);
		//调用增加财务明细的方法
		int rows4 = snFiDetailMapper.insertSelective(snFiDetail);

		if( rows > 0 && rowa > 0){
			return rows;
		}else{
			return 0;
		}
	}
	
	/**
	 * 查询行数
	 */
	@Override
	public int findCount(Map<String, Object> maps) {
		return retailMapper.findCount(maps);
	}
}
